import requests
from lxml import etree
from selenium import webdriver
import time

from selenium.webdriver.common.keys import Keys

url = "https://image.baidu.com/"

def fetch_website(url):
    driver = webdriver.Firefox()
    driver.get(url)
    return driver

def script_execute(driver,js):
    try:
        driver.execute_script(js)
        return True
    except Exception as e:
        print(f"script_execute {js} has error with {e}")
        return False

def selenium_baidu_picture(url):
    driver = fetch_website(url)
    driver.find_element_by_id('kw').send_keys("小鸟")
    driver.find_element_by_id('kw').send_keys(Keys.ENTER)
    time.sleep(5)
    a = 500
    while True:
        js = "window.scrollTo(0,%d)" % a
        script_execute(driver,js)
        time.sleep(2)
        a += 100
    # driver.quit()

if __name__ == "__main__":
    selenium_baidu_picture(url)
